package me.sdevil507.platform.controller;

import lombok.extern.slf4j.Slf4j;
import me.sdevil507.biz.common.exceptions.PhoneNumberExistedException;
import me.sdevil507.org.dto.OrgUserMergeParamsDto;
import me.sdevil507.org.dto.OrgUserParamsDto;
import me.sdevil507.org.dto.OrgUserResultDto;
import me.sdevil507.org.mapper.OrgUserMapper;
import me.sdevil507.org.po.OrgUserPo;
import me.sdevil507.org.service.OrgUserService;
import me.sdevil507.supports.jpa.PageConvertCallBack;
import me.sdevil507.supports.jpa.PageResultDTO;
import me.sdevil507.supports.jpa.PageResultGenerator;
import me.sdevil507.supports.result.ApiResultDTO;
import me.sdevil507.supports.result.ApiResultGenerator;
import me.sdevil507.supports.status.ApiStatusCode;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Plat用户Controller
 *
 * @author sdevil507
 * created on 2020/4/20
 */
@RestController
@RequestMapping("/api/plat/org/user")
@Slf4j
public class PlatOrgUserController {

    @Autowired
    private OrgUserService orgUserService;

    @Autowired
    private OrgUserMapper orgUserMapper;

    /**
     * 创建机构下管理用户--只做新增用户, 确定不了属于哪个机构
     *
     * @param dto 用户dto
     * @return 执行反馈
     */
    @PostMapping("/create")
    @RequiresPermissions("plat:org:user:add")
    public ApiResultDTO create(@RequestBody OrgUserParamsDto dto) {
        try {
            orgUserService.platCreateOrgUser(dto);
        } catch (PhoneNumberExistedException e) {
            return ApiResultGenerator.create(ApiStatusCode.ACCOUNT_EXISTED.getCode(), ApiStatusCode.ACCOUNT_EXISTED.getDescription());
        }
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
    }

    /**
     * 修改个人信息
     *
     * @param dto 参数
     * @return 执行反馈
     */
    @PostMapping("/update")
    @RequiresPermissions("plat:org:user:edit")
    public ApiResultDTO update(@RequestBody OrgUserParamsDto dto) {
        try {
            orgUserService.platUpdateOrgUser(dto);
        } catch (PhoneNumberExistedException e) {
            return ApiResultGenerator.create(ApiStatusCode.ACCOUNT_EXISTED.getCode(), ApiStatusCode.ACCOUNT_EXISTED.getDescription());
        }
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
    }

    /**
     * 删除机构下用户
     *
     * @param dto 参数
     * @return 执行反馈
     */
    @PostMapping("/delete")
    @RequiresPermissions("plat:org:user:delete")
    public ApiResultDTO delete(@RequestBody OrgUserParamsDto dto) {
        orgUserService.delete(dto);
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
    }

    /**
     * 冻结机构下用户
     *
     * @param dto 参数
     * @return 执行反馈
     */
    @PostMapping("/lock")
    @RequiresPermissions("plat:org:user:lock")
    public ApiResultDTO lock(@RequestBody OrgUserParamsDto dto) {

        OrgUserPo orgUserPo = orgUserService.readOneById(dto.getId());
        orgUserPo.setLocked(dto.getLocked());
        orgUserService.updateData(orgUserPo);
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
    }

    /**
     * 分页获取信息列表
     *
     * @param dto 入参参数
     * @return 分页数据
     */
    @SuppressWarnings("Convert2Lambda")
    @PostMapping("/readAllByPage")
    @RequiresPermissions("plat:org:user:view")
    public ApiResultDTO readAllByPage(@RequestBody OrgUserMergeParamsDto dto) {
        Page<OrgUserPo> pagePos = orgUserService.readAllByPage(dto.getSearchDto(), dto.getPageDto(), dto.getSortDto());
        PageResultDTO<OrgUserResultDto> pageResultDto = PageResultGenerator.convert(pagePos, new PageConvertCallBack<OrgUserResultDto, OrgUserPo>() {
            @Override
            public List<OrgUserResultDto> exec(List<OrgUserPo> list) {
                return orgUserMapper.pos2dtos(list);
            }
        });
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), pageResultDto);
    }

}
